package io;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

public class Test0 {
    public static void main(String[] args) throws FileNotFoundException {
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入文件名：");
        String filename=sc.nextLine();
        FileOutputStream fos=new FileOutputStream(filename,true);
        OutputStreamWriter os=new OutputStreamWriter(fos, StandardCharsets.UTF_8);
        BufferedWriter bw=new BufferedWriter(os);
        PrintWriter pw=new PrintWriter(bw,true);//true表示自动行刷新，每次输入内容就写入文件
        while (true){
            System.out.println("请输入内容，exit退出：");
            String line=sc.nextLine();
            if ("exit".equalsIgnoreCase(line)){
                break;
            }
            pw.println(line);
            //pw.flush();
        }
        System.out.println("文件写入完毕，再见");
        pw.close();
    }
}
